package pa165.dominatingspecies.rest;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import pa165.dominating.species.tos.EnvironmentTo;

/**
 *
 * @author Stanislau Dubrouski
 */
public class EnvironmentTableModel extends AbstractTableModel {

    private static enum COLUMNS {

        ID, NAME, DESCRIPTION
    }
    private List<EnvironmentTo> envs = new ArrayList<>();

    @Override
    public int getRowCount() {
        return envs.size();
    }

    @Override
    public int getColumnCount() {
        return COLUMNS.values().length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        EnvironmentTo env = envs.get(rowIndex);
        switch (COLUMNS.values()[columnIndex]) {
            case ID:
                return env.getId();
            case NAME:
                return env.getName();
            case DESCRIPTION:
                return env.getDescription();
            default:
                throw new IllegalArgumentException("column index");
        }
    }

    @Override
    public String getColumnName(int columnIndex) {
        switch (EnvironmentTableModel.COLUMNS.values()[columnIndex]) {
            case ID:
                return "ID";
            case NAME:
                return "Name";
            case DESCRIPTION:
                return "Description";
            default:
                throw new IllegalArgumentException("column index");
        }
    }

    public void addEnvironmentToTable(EnvironmentTo env) {
        envs.add(env);
        int lastRow = envs.size() - 1;
        fireTableRowsInserted(lastRow, lastRow);
    }
    
    public void deleteEnvironmentFromTable(EnvironmentTo env){
        int index = envs.indexOf(env);
        envs.remove(env);
        fireTableRowsDeleted(index, index);
    }
    
    public void updateEnvironmentInTable(EnvironmentTo env){
      int index = envs.indexOf(env);
      fireTableRowsUpdated(index, index);
    }
    
    public EnvironmentTo getSelectedEnvironment(int rowIndex){
        return envs.get(rowIndex);
    }

    public List<EnvironmentTo> getEnvironments() {
        return envs;
    }
}
